<?php
{
    /**
     * Class Base Model基类
     */
    abstract class Base
    {
        protected $logHelper = null;
        protected $sqlHelper = null;
        protected $tableName = null;


        function __construct()
        {
            $this -> logHelper = new LogHelper();
            $this -> sqlHelper = new SqlHelper();
            $this -> tableName = get_called_class();
        }

        /**
         * 根据ID读取指定表信息
         * @param string $id 编号
         * @return array 结果集
         * @throws Exception
         */
        public function GetModelByID($id)
        {
            $sql = "SELECT * FROM " . $this -> tableName . " WHERE ID = " . $id;
            $results = $this -> sqlHelper -> getAll($sql);
            if (count($results) == 0)
                return null;
            return $results[0];
        }

        /**
         * 根据ID读取指定表信息
         * @param string $id 编号
         * @return bool 结果集
         * @throws Exception
         */
        public function DeleteModelByID($id)
        {
            $sql = "DELETE FROM " . $this -> tableName . " WHERE ID = " . $id;
            $results = $this -> sqlHelper -> Query($sql);
            if ($results == 0)
            {
                return false;
            }
            return true;
        }


        /**
         * 执行修改操作
         * @param array $parms 参数信息
         * @return bool 执行是否成功
         * @throws Exception
         */
        public function UpdateModel($parms)
        {
            $sql = "UPDATE " . $this -> tableName . " SET ";
            $id = "";
            foreach ($parms as $key => $val)
            {
                if ($key == "ID" || $key == "id")
                {
                    $id = $val;
                    continue;
                }
                $sql .= "`" . $key . "`='" . $val . "',";
            }
            $sql = substr($sql, 0, strlen($sql) - 1);
            $sql .= " WHERE ID = " . $id;
            $results = $this -> sqlHelper -> Query($sql);
            if ($results == 0)
            {
                return false;
            }
            return true;
        }

        /**
         * 执行添加操作
         * @param array $parms 参数信息
         * @return bool 执行是否成功
         * @throws Exception
         */
        public function InsertModel($parms)
        {
            $columns = "";
            $values = "";
            foreach ($parms as $key => $val)
            {
                $columns .= "`" . $key . "`,";
                $values .= "'" . $val . "',";
            }

            $columns = substr($columns, 0, strlen($columns) - 1);
            $values = substr($values, 0, strlen($values) - 1);

            $sql = "INSERT INTO " . $this -> tableName . "(" . $columns . ") VALUES(" . $values . ")";
            $results = $this -> sqlHelper -> Query($sql);
            if ($results == 0)
            {
                return false;
            }
            return true;
        }

    }
}
?>